<template>
  <div>
    <el-form ref="myform" :model="formItem" :rules="rules" label-width="120px" size="mini">
      <el-form-item label="备品备件分类" prop="spareTypeCode">
        <TreeSelect
          v-model="formItem.spareTypeCode"
          :multiple="false"
          :options="typeOptions"
          placeholder="选择备件类型"
        />
      </el-form-item>
      <el-form-item label="货品名称" prop="name">
        <el-input v-model="formItem.name" placeholder="请输入货品名称" />
      </el-form-item>
      <el-form-item label="规格型号" prop="specification">
        <el-input v-model="formItem.specification" placeholder="请输入规格型号" />
      </el-form-item>
      <!-- <el-form-item label="计量单位" prop="measureUnit">
        <el-input v-model="formItem.measureUnit" placeholder="请输入计量单位" />
      </el-form-item> -->
      <el-form-item label="货品描述" prop="description">
        <el-input v-model="formItem.description" placeholder="请输入货品描述" />
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {
  create,
  update,
  detail
} from '@neu/dcam-ui/api/goodsFile'

import TreeSelect from '@neu/biz-core/components/TreeSelect'

import {
  tree
} from '@neu/dcam-ui/api/spareType'

// 定义一个表单原始项
const formItem = {
  spareTypeCode: null,
  name: '',
  specification: '',
  // measureUnit: '',
  description: ''
}
export default {
  name: 'GoodsFileEditForm',
  components: {
    TreeSelect
  },
  props: {
    // 根据id可判断是新建还是更新
    id: {
      required: false,
      type: Number,
      default: -1
    }
  },
  data() {
    return {
      // 控件类型
      controlTypeList: [],
      // 文本格式下拉框
      valueTypeList: [],
      // 编辑的表单内容
      formItem: {
        ...formItem
      },
      // 表单验证规则
      rules: {
        name: [
          { required: true, message: '货品名称不能为空', trigger: 'blur' },
          { max: 225, message: '长度不能超过225', trigger: 'blur' }
        ],
        specification: [
          { required: true, message: '规格型号不能为空', trigger: 'blur' },
          { max: 225, message: '长度不能超过225', trigger: 'blur' }
        ],
        spareTypeCode: [
          { required: true, message: '备品备件分类不能为空', trigger: 'blur' }
        ]
      },
      modal: true,
      /** 备件类型树 */
      typeOptions: []
    }
  },
  watch: {
    // 监控id的变化，初始化表单数据
    id: {
      handler(newVal, oldVal) {
        this.fetch()
      },
      immediate: true
    }
  },
  created() {
    this.tree()
  },
  methods: {
    // 类型树
    tree() {
      tree().then(response => {
        this.typeOptions = response.data
      })
    },
    // 初始化表单数据
    fetch() {
      if (this.id !== -1) {
        detail(this.id).then(response => {
          this.formItem = {
            ...response.data
          }
        })
      } else {
        this.formItem = {
          ...formItem
        }
      }
    },
    // 保存操作，返回promise，后续处理交由调用者处理
    save() {
      const updated = this.id !== -1
      return new Promise(resolve => {
        this.$refs.myform.validate((valid) => {
          if (valid) {
            if (updated) {
              resolve(update(this.id, this.formItem))
            } else {
              resolve(create(this.formItem))
            }
          } else {
            resolve(false)
          }
        })
      })
    },
    reset() {
      this.$refs.myform.resetFields()
    }
  }
}
</script>
